package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.interceptor.LoginInterceptor;
import com.ruoyi.mapper.MyShortFlowingWaterMapper;
import com.ruoyi.mapper.MyShortVideoMapper;
import com.ruoyi.model.LoginUser;
import com.ruoyi.model.MyShortFlowingWaterDO;
import com.ruoyi.model.MyShortVideo;
import com.ruoyi.request.MyShortFlowingWaterRequest;
import com.ruoyi.service.MyShortFlowingWater;
import com.ruoyi.util.JsonData;
import com.ruoyi.vo.MyShortFlowingWaterVO;
import com.ruoyi.vo.MyShortVideoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zunzun
 * @since 2023-08-11
 */

@Service
@Slf4j
public class MyShortFlowingWaterImpl implements MyShortFlowingWater {

    @Autowired
    private MyShortFlowingWaterMapper myShortFlowingWaterMapper;

    @Override
    public JsonData addDetail(MyShortFlowingWaterDO myShortFlowingWaterDO) {

        return JsonData.buildSuccess(myShortFlowingWaterMapper.insert(myShortFlowingWaterDO));
    }

    @Override
    public MyShortFlowingWater findDetail(int id) {
        return null;
    }

    @Override
    public Map<String, Object> selectMyShortFlowingWaterList(MyShortFlowingWaterRequest myShortFlowingWaterRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();

        //封装分页信息
        Page<MyShortFlowingWaterDO> pageInfo = new Page<>(myShortFlowingWaterRequest.getPageIndex(), myShortFlowingWaterRequest.getPageNumber());

        IPage<MyShortFlowingWaterDO> recordDOIPage = myShortFlowingWaterMapper.selectPage(pageInfo, new QueryWrapper<MyShortFlowingWaterDO>()
                .eq("user_id", loginUser.getId())
                .orderByDesc("create_time"));

        log.info("recordDOIPage = {}", recordDOIPage);
        Map<String, Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", recordDOIPage.getTotal());
        pageMap.put("total_page", recordDOIPage.getPages());
        pageMap.put("current_data", recordDOIPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));

        return pageMap;
    }

    private MyShortFlowingWaterDO beanProcess(MyShortFlowingWaterDO myShortFlowingWaterDO) {
        MyShortFlowingWaterVO myShortFlowingWaterVO = new MyShortFlowingWaterVO();
        BeanUtils.copyProperties(myShortFlowingWaterDO, myShortFlowingWaterVO);
        return myShortFlowingWaterDO;
    }
}
